package com.xiaobias.user.web;

import com.xiaobias.user.config.PatternProperties;
import com.xiaobias.user.pojo.User;
import com.xiaobias.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping(value = {"user"})
//@RefreshScope
public class UserController {

    private final UserService userService;

    private final PatternProperties patternProperties;

//    @Value("${pattern.dateformat}")
//    private String dateformat;

    @Autowired
    public UserController(UserService userService, PatternProperties patternProperties) {
        this.userService = userService;
        this.patternProperties = patternProperties;
    }

    @GetMapping(value = {"prop"})
    public PatternProperties properties() {
        return patternProperties;
    }

    @GetMapping(value = {"/now"})
    public String now() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    /**
     * 根据ID查询用户
     */
    @GetMapping(value = {"{userId}"})
    public User queryById(@PathVariable("userId") Long userId) {
        // 查询用户并返回
        return this.userService.queryById(userId);
    }
}
